package com.devunion.salon.persistence.dao;

import com.devunion.salon.persistence.Location;

import java.io.Serializable;
import java.sql.SQLException;

import org.springframework.orm.hibernate3.HibernateCallback;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Hibernate;

/**
 * @author Viktar Liaskovich
 */
public class LocationDao extends BaseDaoObject<Location> {
    public Location getByKey(Serializable key) {
        return (Location) getHibernateTemplate().get(Location.class, key);
    }

    public Location getLocation(final String username) {
        return (Location) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                return session.createQuery("select login.location from Login login where " +
                        "login.login = :username")
                        .setParameter("username", username, Hibernate.STRING).list().get(0);
            }
        });
    }
}